package tk.djcrazy.instantmessage.action;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;

import tk.djcrazy.instantmessage.domain.UserEntity;
import tk.djcrazy.instantmessage.service.IUserAvatarService;
import tk.djcrazy.instantmessage.service.IUserService;

import com.opensymphony.xwork2.ActionSupport;

public class SaveProfileAction extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = -5855329854120296716L;
	private String statue;
	private File avatar;
	private String avatarFileName;
	private String result="failed";
	private String message;
	
	@Resource
	private IUserService userService;
	@Resource
	private IUserAvatarService userAvatarService;
	private Map<String, Object> session;

	public String execute() {
		try {
			if (avatar!=null) {
				String path ="avatarfile/"+ userAvatarService.storeAvatar(avatar, avatarFileName);
				userAvatarService.saveUserAvatar((Integer) session.get("uid"), path);
				session.put("avatar", path);
			}
			UserEntity entity = userService.findById((Integer) session.get("uid"));
			entity.setStatue(statue);
			userService.save(entity);
			result="success";
		} catch (IOException e) {
			e.printStackTrace();
			result="failed";
 		}
		return SUCCESS;
	}

	public String getStatue() {
		return statue;
	}

	public void setStatue(String statue) {
		this.statue = statue;
	}

	public File getAvatar() {
		return avatar;
	}

	public void setAvatar(File avatar) {
		this.avatar = avatar;
	}

	public String getAvatarFileName() {
		return avatarFileName;
	}

	public void setAvatarFileName(String avatarFileName) {
		this.avatarFileName = avatarFileName;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public IUserAvatarService getUserAvatarService() {
		return userAvatarService;
	}

	public void setUserAvatarService(IUserAvatarService userAvatarService) {
		this.userAvatarService = userAvatarService;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
